package hash;

import org.junit.Test;

import java.util.HashMap;

/*
 *@description 赎金信
 *@author hkfan
 *@version 1.0
 *@create 2025/5/28 15:08
 */
public class LeetCode_383 {

    @Test
    public void test(){
        System.out.println(canConstruct("aab","baa"));
    }
    public boolean canConstruct(String ransomNote, String magazine) {
        //1. 统计magazine中的字符
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < magazine.length(); i++) {
            map.put(magazine.charAt(i),map.getOrDefault(magazine.charAt(i),0)+1);
        }
        //2. 逐个判断ransomNote中的字符
        for (int i = 0; i < ransomNote.length(); i++) {
            if (map.get(ransomNote.charAt(i))==null||map.get(ransomNote.charAt(i))<=0){
                return false;
            }else {
                map.put(ransomNote.charAt(i),map.get(ransomNote.charAt(i))-1);
            }
        }
        return true;
    }
}
